package com.liuhaozzu.zookeeper.zookeeper_app.node;


import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Version;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
@SuppressWarnings("unused")
public class CreateNode implements Watcher{

	private static CountDownLatch connectedSemaphore=new CountDownLatch(1);
	private static String connectString="hadoop1:2181";
	private static int  sessionTimeout=5000;
	static ZooKeeper zooKeeper;
	public static void main(String[] args) throws Exception {
		//createNodeSync();
		//createNodeAsync();
		deleteNode();
		//getChildren();
		 
	}
	private static void getChildren() throws IOException, InterruptedException,
			KeeperException {
		String path="/liuhaozzu";
		zooKeeper=new ZooKeeper(connectString,5000,new CreateNode());
		connectedSemaphore.await();
		zooKeeper.create(path, "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		zooKeeper.create(path+"/c1", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
		List<String> childrenList=zooKeeper.getChildren(path, true);
		System.out.println(childrenList);
		zooKeeper.create(path+"c2", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
		
		Thread.sleep(Integer.MAX_VALUE);
	}
	private static void deleteNode() throws IOException, InterruptedException,
			KeeperException {
		zooKeeper=new ZooKeeper(connectString,5000,new CreateNode());
		zooKeeper.delete("/liuhaozzu", 1);
	}
	private static void createNodeAsync() throws IOException,
			InterruptedException {
		zooKeeper=new ZooKeeper(connectString,5000,new CreateNode());
		connectedSemaphore.await();
		zooKeeper.create("/liuhaozzu-test-ephemeral--", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL,new IStringCallback(),"I am context");
		zooKeeper.create("/liuhaozzu-test-ephemeral--", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL,new IStringCallback(),"I am context");
		zooKeeper.create("/liuhaozzu-test-ephemeral--", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL,new IStringCallback(),"I am context");
		
		Thread.sleep(Integer.MAX_VALUE);
	}
	
	private static void createNodeSync() throws IOException,
			InterruptedException, KeeperException {
		zooKeeper=new ZooKeeper(connectString ,5000, new CreateNode());
		connectedSemaphore.await();
		String path1=zooKeeper.create("/liuhaozzu-test-ephemeral-", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
		System.out.println("Success create znode:"+path1);
		
		String path2=zooKeeper.create("/liuhaozzu-test-ephemeral-", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
		System.out.println("Success create znode:"+path2);
	}
	@Override
	public void process(WatchedEvent event) {
		if (KeeperState.SyncConnected==event.getState()) {
			if(EventType.None==event.getType() && null == event.getPath()){
				connectedSemaphore.countDown();
			} else if (event.getType()== EventType.NodeChildrenChanged) {
				try {
					System.out.println("Reget Child:"+ zooKeeper.getChildren(event.getPath(), true));
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
	}
}
class IStringCallback implements AsyncCallback.StringCallback{

	@Override
	public void processResult(int rc, String path, Object ctx, String name) {
		System.out.println("create path result:["+rc+", "+path+", "+ ctx+", real path name: "+name);
		
	}
	
}
